package org.reenscape.sock;
import org.reenscape.Server;
import java.net.*;
import java.io.*;
/**
* Creates a socket that the website connects to and reads the number of players currently on.
*/
public class PlayerCount
{
	/**
	* Port socket runs on.
	*/
	private static int SocketPort = 43597;
	
	/**
	* Socket used by the current connected computer.
	*/
	private static Socket sock;
	
	/**
	* Accepts connections on to the socket.
	*/
	private static ServerSocket serversock;
	
	/**
	* Starts the thread that controls the listener.
	*/
	public static void go()
	{
		Runnable r = new Accepter();
		Thread t = new Thread(r);
		t.start();
	}
	
	/**
	* The class that contains the listener.
	*/
	public static class Accepter implements Runnable
	{
		/**
		* Accepts the connection and writes the amount of players to the socket.
		*/
		public void run()
		{
			try
			{
				serversock = new ServerSocket(SocketPort);
				while(true)
				{
					sock = serversock.accept();
					PrintWriter writer = new PrintWriter(sock.getOutputStream());
					String players_ = ""+Server.clientUpdater.getPlayerCount();
					writer.println(players_);
					writer.close();
				}
			} catch(Exception e) { }
		}
	}
}